using System.Net;
using System.Web;
using System.Xml;

namespace Toast.Compression.Providers
{
    internal class GoogleClosure
    {
        const string Data = "js_code={0}&output_format=xml&output_info=compiled_code&compilation_level={1}";
        const string Url = "http://closure-compiler.appspot.com/compile";

        internal static string Compress(string content, string compilationLevel)
        {
            var wClient = new WebClient();
            wClient.Headers.Add("content-type", "application/x-www-form-urlencoded");
            var apiData = string.Format(Data, HttpUtility.UrlEncode(content), compilationLevel);
            var response = wClient.UploadString(Url, apiData);
            var xml = new XmlDocument();
            xml.LoadXml(response);
            return xml.SelectSingleNode("//compiledCode").InnerText;
        }
    }
}